/**
 * <p>Title: ServiceZMCE41.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          22011-12-19 下午02:40:20  高强  Create
 *</pre>
 */
package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.zm.ce.domain.ZMCEMaintenanceApply;

/**
 * <p>Title: ServiceZMCE41.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author  高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-19 下午02:40:20  高强  Create
 *</pre>
 */
public class ServiceZMCE41 extends ServiceEPBase {
	
	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = null;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = query(inInfo);
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.ce.empName", "empCode", "empName", true));//人员
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.department", "orgCode", "orgName", false));
		outInfo.addBlock(Equipment.getEquipmentBlock("cmes.ce.equipment", null));
		
		outInfo.set("userId", UserSession.getUserId());
		outInfo.set("userName", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("departmentId", UserSession.getInSessionProperty("orgCode"));
		outInfo.set("departmentName", UserSession.getInSessionProperty("orgName"));
		return outInfo;
	}

	/**
	 * 设备变动查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdDepartmentId = UserSession.getInSessionProperty("orgCode") == null ? "" : UserSession.getInSessionProperty("orgCode").toString();
		StaticTools.setQueryParameters(inInfo, "fdDepartmentId", fdDepartmentId, true);//添加查询条件 操作用户
		EiInfo outInfo = super.query(inInfo, "ZMCEMaintenanceApply.query", new ZMCEMaintenanceApply());				
		return outInfo;
	}
	
	/**
	 * 设备变动新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validateValue = validate(inInfo, "ZMCEMaintenanceApply.validate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_MAINTENANCEAPPLY"));//主键
			inInfo.getBlock("result").setCell(0, "fdStatus", "待处理");
			EiInfo outinfo = super.insert(inInfo, "ZMCEMaintenanceApply.insert", "result");
			return outinfo;
		}
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCEMaintenanceApply.update");
		return outInfo;
	}
	

	/**
	 * 设备变动删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE31</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-19 下午02:40:20  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEMaintenanceApply.delete");
		EiInfo outInfo = query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());
		
		return outInfo;
	}


	
	/**
	 * 构造状态下拉框
	 * <p>Function: getStatus</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-8下午04:14:57 余虎
	 * </p>
	 *
	 */
//	@SuppressWarnings("rawtypes")
//	private EiBlock getStatus()
//	{
//		EiBlock block = new EiBlock("cmes.cs.status");
//		
//		EiColumn eiColumn = new EiColumn("label");
//		eiColumn.setType("C");
//		block.addMeta(eiColumn);
//		
//		eiColumn = new EiColumn("value");
//		eiColumn.setType("C");
//		block.addMeta(eiColumn);
//		
//		List<Map> list = new ArrayList<Map>();
//		
//		Map<String, String> _map = new HashMap<String, String>();
//		_map.put("value", "待处理");
//		_map.put("label", "待处理");
//		list.add(_map);
//		
//		_map = new HashMap<String, String>();
//		_map.put("value", "已生成计划");
//		_map.put("label", "已生成计划");
//		list.add(_map);
//		
//		_map = new HashMap<String, String>();
//		_map.put("value", "已生成工单");
//		_map.put("label", "已生成工单");
//		list.add(_map);
//		
//		_map = new HashMap<String, String>();
//		_map.put("value", "作废");
//		_map.put("label", "作废");
//		list.add(_map);
//		
//		block.setRows(list);
//		return block;
//	}
	
	
	/**
	 * 校验重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-26下午04:26:16 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdCode = inInfo.getBlock("result").getCellStr(0, "fdCode");
		String fdEquipmentId = inInfo.getBlock("result").getCellStr(0, "fdEquipmentId");
		params.put("fdCode", fdCode);
		
		List result = dao.query("ZMCEMaintenanceApply.validate", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("编号不能重复！");
			return true;			
		}
		Map<String, String> params1 = new HashMap<String, String>();
		params1.put("fdEquipmentId", fdEquipmentId);
		List result1 = dao.query("ZMCEMaintenanceApply.validate", params1);
		if(result1!=null && result1.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("同一个设备的申请在没有被处理前，不能新增！");
			return true;			
		}
		return false;
	}
}
